package com.csw;

import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/22 22:32
 */
public class GameWin extends JFrame {
    int wight = 2 * GameUtil.OFFSET + GameUtil.Map_W * GameUtil.SQUARE_LENGTH;
    int height = 4 * GameUtil.OFFSET + GameUtil.Map_H * GameUtil.SQUARE_LENGTH;
    //双缓存画布
    Image offScreenImage;
    //是否点击到
    Boolean isSelect=false;
    MapBottom mapBottom = new MapBottom();
    GameSelect gameSelect=new GameSelect();
    MapTop mapTop=new MapTop();
    void launch() {
        GameUtil.startTime=System.currentTimeMillis();
        this.setVisible(true);
        if (GameUtil.state==3){
            this.setSize(500, 500);
        }else {
            this.setSize(wight, height);
        }
        this.setTitle("扫雷");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //鼠标事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (GameUtil.state){
                    case 0:
                        //鼠标左键点击
                        if (e.getButton()==1){
                            GameUtil.MOUSE_X=e.getX();
                            GameUtil.MOUSE_Y=e.getY();
                            GameUtil.LEFT=true;
                        }
                        //鼠标右键点击
                        if (e.getButton()==3){
                            GameUtil.MOUSE_X=e.getX();
                            GameUtil.MOUSE_Y=e.getY();
                            GameUtil.RIGHT=true;
                        }

                    case 1:
                    case 2:
                        //利用switch的穿透,任何情况下点击笑脸都可以重置游戏
                        if (e.getButton()==1){
                            if (e.getX()>GameUtil.OFFSET+GameUtil.SQUARE_LENGTH*(GameUtil.Map_W/2)
                            &&e.getX()<GameUtil.OFFSET+GameUtil.SQUARE_LENGTH*(GameUtil.Map_W/2)+GameUtil.SQUARE_LENGTH
                            &&e.getY()>GameUtil.OFFSET
                            &&e.getY()<GameUtil.OFFSET+GameUtil.SQUARE_LENGTH){
                                mapBottom.regame();
                                mapTop.regame();
                                GameUtil.startTime=System.currentTimeMillis();
                                GameUtil.flagCount=0;
                                GameUtil.state=0;
                            }
                        }
                        //按了滚轮开始选择难度
                        if (e.getButton()==2){
                            GameUtil.state=3;
                            isSelect=true;
                        }
                        break;
                    case 3:
                        if (e.getButton()==1){
                            GameUtil.MOUSE_X=e.getX();
                            GameUtil.MOUSE_Y=e.getY();
                            isSelect = gameSelect.hard();
                        }
                        break;
                }


            }
        });


        while (true){
            repaint();
            begin();
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    void begin(){
        if (isSelect){
            isSelect=false;
            gameSelect.hard(GameUtil.level);
            dispose();
            GameWin gameWin = new GameWin();
            GameUtil.startTime=System.currentTimeMillis();
            GameUtil.flagCount=0;
            mapBottom.regame();
            mapTop.regame();
            gameWin.launch();
        }
    }

    @Override
    public void paint(Graphics g) {
        if (GameUtil.state==3){
            offScreenImage=this.createImage(800, 800);
            Graphics og = offScreenImage.getGraphics();
            og.setColor(Color.white);
            og.fillRect(0, 0, 800, 800);
            gameSelect.paint(og);
            g.drawImage(offScreenImage,0,0, 800, 800, null);
        }else {
        offScreenImage=this.createImage(wight, height);
        Graphics og = offScreenImage.getGraphics();
        mapBottom.paintSelf(og);
        mapTop.paintSelf(og);
        g.drawImage(offScreenImage,0,0, wight, height, null);
        }
    }

    public static void main(String[] args) {
        new GameWin().launch();
    }
}
